﻿namespace ZCUtil.Algorithm
{
    /// <summary>
    /// T的撤销队列
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseQueue<T> : Queue
    {
        #region ProtectedField 数据和重做
        protected T[] m_Arrays;
        protected int m_Redo;
        #endregion

        #region PublicMethod 记录操作
        public BaseQueue(int size) : base(size + 1)
        {
            m_Arrays = new T[size + 1];
            m_Redo = 0;
        }

        public void Initialize(T t)
        {
            if (GetCurrent() == null)
            {
                CreateCurrent(t);
                CopyCurrent(t);
            }
            m_Head++;
            m_Tail++;
        }

        public T GetLast()
        {
            return m_Arrays[(m_Tail - 1 + m_Size) % m_Size];
        }

        public T GetCurrent()
        {
            return m_Arrays[m_Tail];
        }


        public void Next(T t)
        {
            if (GetCurrent() == null)
            {
                CreateCurrent(t);
            }
            CopyCurrent(t);
            After();
            m_Redo = 0;
        }
        #endregion

        #region ProtectedMethod 创建和拷贝数据
        protected virtual void CreateCurrent(T t)
        {

        }

        protected virtual void CopyCurrent(T t)
        {

        }
        #endregion

        #region PublicMethod Undo和Redo
        public bool CanUndo()
        {
            return !Empty();
        }

        public void Undo()
        {
            if (!CanUndo())
                return;
            Front();
            m_Redo++;
        }

        public bool CanRedo()
        {
            return m_Redo > 0;
        }

        public void Redo()
        {
            if (!CanRedo())
                return;
            After();
            m_Redo--;
        }
        #endregion
    }
}